/*  
 * Copyright 2007 MBARI 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package org.mbari.timeline;

import java.util.Calendar;
import java.util.TimeZone;

import com.domainlanguage.time.TimeInterval;
import com.domainlanguage.time.TimePoint;

/** 
 * @author Andrew Chase
 */
public class DateUtil {
    public static final TimeZone GMT_TIMEZONE = TimeZone.getTimeZone("GMT");
    //mainly intended as a handy dummy date for test purposes.
	public static final TimePoint BEGIN_2007= ansiStandardDateStringToDate(
												"20070101-00:00:00");
    
	/**
	 * return a Date representing the date encoded in the string provided
	 * This is very brittle and expects perfectly formatted input, not to be used
	 * for user entered values.
	 * 
	 * @param date YYYYMMDD-HH:MM:SS example : 20070417-22:07:45.
	 * @return A Date object representing the String provided. String to date
	 * conversion using GMT timezone and a 24 hour clock (1-24)
	 */
	public static TimePoint ansiStandardDateStringToDate(String date){
		return TimePoint.parseGMTFrom(date, "yyyyMMdd-kk:mm:ss");
	}
	
	public static long lengthInMillis(TimeInterval interval){
	    return interval.end().asJavaUtilDate().getTime()- interval.start().asJavaUtilDate().getTime();
	}
	
	public static int extractDay(TimePoint time){
	    return time.asJavaCalendar().get(Calendar.DAY_OF_MONTH);
	}
	
	public static int extractMonth(TimePoint time){
	    return time.asJavaCalendar().get(Calendar.MONTH) + 1;
	}
}
